{% extends "base.html" %}

{% block title %}搜索结果 - {{ super() }}{% endblock %}

{% block content %}
<div class="container">
    <div class="card mb-4">
        <div class="card-body">
            <form method="get" action="{{ url_for('community.search') }}" class="row g-3">
                <div class="col-md-8">
                    <div class="input-group">
                        <input type="text" class="form-control" name="keyword" 
                               placeholder="搜索帖子..." value="{{ keyword }}">
                        <button class="btn btn-primary" type="submit">
                            <i class="fas fa-search"></i> 搜索
                        </button>
                    </div>
                </div>
                <div class="col-md-4">
                    <select class="form-select" name="sort" onchange="this.form.submit()">
                        <option value="newest" {% if sort == 'newest' %}selected{% endif %}>最新发布</option>
                        <option value="views" {% if sort == 'views' %}selected{% endif %}>最多浏览</option>
                        <option value="comments" {% if sort == 'comments' %}selected{% endif %}>最多评论</option>
                    </select>
                </div>
            </form>
        </div>
    </div>

    {% if keyword %}
    <h5 class="mb-4">搜索 "{{ keyword }}" 的结果 ({{ posts.total }})</h5>
    {% endif %}

    <div class="row">
        {% for post in posts.items %}
        <div class="col-12 mb-4">
            <div class="card">
                <div class="card-body">
                    <h5 class="card-title">
                        <a href="{{ url_for('community.detail', id=post.id) }}" class="text-decoration-none">
                            {{ post.title }}
                        </a>
                    </h5>
                    <p class="card-text text-muted">{{ post.content[:200] }}...</p>
                    <div class="d-flex justify-content-between align-items-center">
                        <div class="text-muted">
                            <span><i class="fas fa-user"></i> {{ post.user.username }}</span>
                            <span class="ms-3"><i class="fas fa-eye"></i> {{ post.views }}</span>
                            <span class="ms-3"><i class="fas fa-comments"></i> {{ post.comments|length }}</span>
                        </div>
                        <small class="text-muted">
                            <i class="fas fa-clock"></i> {{ post.created_at.strftime('%Y-%m-%d %H:%M') }}
                        </small>
                    </div>
                </div>
            </div>
        </div>
        {% else %}
        <div class="col-12">
            <div class="alert alert-info text-center">
                {% if keyword %}
                    没有找到包含 "{{ keyword }}" 的帖子
                {% else %}
                    暂无帖子内容
                {% endif %}
            </div>
        </div>
        {% endfor %}
    </div>

    {% if posts.pages > 1 %}
    <nav aria-label="Page navigation" class="mt-4">
        <ul class="pagination justify-content-center">
            {% for page_num in posts.iter_pages() %}
                {% if page_num %}
                    {% set args = request.args.copy() %}
                    {% set _ = args.pop('page', None) %}
                    <li class="page-item {% if page_num == posts.page %}active{% endif %}">
                        <a class="page-link" href="{{ url_for('community.search', page=page_num, **args) }}">{{ page_num }}</a>
                    </li>
                {% else %}
                    <li class="page-item disabled"><span class="page-link">...</span></li>
                {% endif %}
            {% endfor %}
        </ul>
    </nav>
    {% endif %}
</div>
{% endblock %} 